package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.api.admin.IWmNewsClient;
import com.heima.model.admin.dtos.UserAuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.CollectionBehaviorDto;

import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.CollectionBehavior;

import com.heima.model.user.pojos.UserRealName;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.AuthMapper;
import com.heima.user.mapper.CollectionBehaviorMapper;

import com.heima.model.wemedia.pojos.WmNews;

import com.heima.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;


@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements UserService {

    @Autowired
    private IWmNewsClient wmNewsClient;

    private final AuthMapper authMapper;

    private final CollectionBehaviorMapper collectionBehaviorMapper;

    private final ApUserMapper apUserMapper;

    @Override
    public ResponseResult userFollow() {
        return null;
    }

    /**
     * 分页查询列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getList(UserAuthDto dto) {
        //校验分页参数
        dto.checkParam();

        //分页查询用户认证表
        IPage page = new Page(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<ApUser> wrapper = Wrappers.<ApUser>lambdaQuery()
                .eq(ObjectUtils.isNotEmpty(dto.getStatus()), ApUser::getStatus, dto.getStatus())
                .eq(ObjectUtils.isNotEmpty(dto.getId()), ApUser::getId, dto.getId())
                .orderByDesc(ApUser::getCreatedTime);
        page(page,wrapper);

        //封装参数
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());

        result.setData(page.getRecords());
        return result;
    }

    /**
     * 审核失败
     *
     * @param userAuthDto
     * @return
     */
    @Override
    public ResponseResult authFail(UserAuthDto userAuthDto) {
        UserRealName user = new UserRealName();
        BeanUtils.copyProperties(userAuthDto, user);
        Date date = new Date();
        user.setUpdatedTime(date);
        user.setSubmitedTime(date);
        user.setStatus((int)WmNews.Status.FAIL.getCode());
        authMapper.updateById(user);
        return ResponseResult.okResult("驳回成功!");
    }


    /**
     * 审核通过
     * 1.user服务数据库中对应的UserRealName的状态更新
     * 2.调用wemedia微服务 完成we_user数据表的新增用户
     */
    @Override
    public ResponseResult authPass(UserAuthDto userAuthDto) {
        //1.user服务数据库中对应的UserRealName的状态更新
        UserRealName user = new UserRealName();
        BeanUtils.copyProperties(userAuthDto, user);
        Date date = new Date();
        user.setUpdatedTime(date);
        user.setSubmitedTime(date);
        user.setStatus((int)WmNews.Status.PUBLISHED.getCode());
        authMapper.updateById(user);
        // 2.调用wemedia微服务 完成we_user数据表的新增用户

        ApUser apUser = apUserMapper.selectById(userAuthDto.getId());
        wmNewsClient.updateUser(apUser);
        return ResponseResult.okResult("审核成功!");
    }

    /**
     * 用户收藏
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult userCollection(CollectionBehaviorDto dto) {
        CollectionBehavior collectionBehavior = new CollectionBehavior();
        BeanUtils.copyProperties(dto,collectionBehavior);
        collectionBehavior.setUserId(dto.getUserId());
        collectionBehaviorMapper.updateById(collectionBehavior);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
